package com.lijing.arrayQueue;

/**
 * 数组队列
 */
public class ArrayQueue {
    private int maxSize;
    private int front;
    private int rear;
    private int arr[];

    //构造方法
    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;//队列的大小
        this.front = -1;//指向第一个元素之前的位置
        this.rear = -1;//指向最后一个元素的位置
        this.arr = new int[maxSize];
    }
    //判断队列是否已满
    public boolean isFull(){
        return rear == maxSize - 1;
    }
    //判断队列是否为空
    public boolean isEmpty(){
        return front == rear;
    }

    //加入元素
    public void addArrayQueue(int data){
        if(this.isFull()){//如果队列已满，无法添加元素
            System.out.println("Queue is Full!");
            return;
        }
        rear++;
        arr[rear] = data;
    }

    //取出元素
    public int getParam(){
        if(this.isEmpty()){//队列为空，无法取出元素
            throw new RuntimeException("队列为空，无法取出元素");
        }
        front ++;
        return arr[front];
    }
    //遍历队列中的元素
    public void showParam(){
        for (int i = front +1; i < maxSize; i++) {
            System.out.printf("arr[%d] = %d\n",i,arr[i]);
        }
    }
    //显示队列的头数据
    public int peekQueue(){
        if(this.isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return arr[front + 1];

    }
}
